package com.wzh.action.control.collect;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wzh.bean.QueryResult;
import com.wzh.bean.book.BookChapter;
import com.wzh.bean.book.BookInfo;
import com.wzh.bean.book.ContentUrl;
import com.wzh.bean.collect.SiteInfo;
import com.wzh.constant.CommonConstant;
import com.wzh.service.bookinfo.BookInfoService;
import com.wzh.service.bookinfo.chapter.BookChapterService;
import com.wzh.service.contenturl.ContentUrlService;
import com.wzh.service.siteinfo.SiteInfoService;
import com.wzh.utils.BookCollectUtil;
import com.wzh.utils.CommonUtil;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.servlet.ServletContext;

import com.wzh.utils.HtmlJsoupUtil;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

@Controller
public class CollectAction extends ActionSupport {
    private static final long serialVersionUID = 1L;

    @Resource
    private SiteInfoService siteInfoService;

    @Resource
    private ContentUrlService contentUrlService;

    @Resource
    private BookInfoService bookInfoService;

    @Resource
    private BookChapterService bookChapterService;
    private SiteInfo siteinfo;
    private int maxresult = 5;

    public SiteInfo getSiteinfo() {
        return this.siteinfo;
    }

    public void setSiteinfo(SiteInfo siteinfo) {
        this.siteinfo = siteinfo;
    }

    public String startCollect() {
        this.siteinfo = ((SiteInfo) this.siteInfoService.find(this.siteinfo.getId()));
        if (this.siteinfo != null) {
            this.siteInfoService.replaceContent(this.siteinfo);
            String[] listurls = this.siteInfoService.getListUrls(this.siteinfo);

            if (listurls != null) {
                Logger.getAnonymousLogger().info("==================startcollect - webStr:" + this.siteinfo.getDomain() + "开始 循环栏目列表");
                for (String listurl : listurls) {
                    String webStr =
                            BookCollectUtil.getWebContentByUrl(listurl, this.siteinfo.getCharset());
                    Logger.getAnonymousLogger().info("==================startcollect - webStr:" + listurl + "开始 获取小说 待采集列表");
                    Logger.getAnonymousLogger().info("==================startcollect - webStr:" + this.siteinfo.getContentUrlReg() + "开始 正则表达式获取 url");


                    List contentUrls = HtmlJsoupUtil.getBookUrl(webStr, this.siteinfo.getDomain(), this.siteinfo.getCharset());//jsoup 智能获取 bookurl
                    if (contentUrls == null || contentUrls.size() == 0) {
                        contentUrls = BookCollectUtil.getUrls(this.siteinfo.getContentUrlReg(), webStr, this.siteinfo.getDomain());
                    }
                    Logger.getAnonymousLogger().info("==================startcollect - contentUrls:" + contentUrls.size());

                    this.contentUrlService.addContentUrls(contentUrls, this.siteinfo.getId());
                }

                Map regs = this.siteInfoService.getRegMap(this.siteinfo);
                ActionContext.getContext().getSession().put("regs", regs);
                ActionContext.getContext().getSession().put("siteinfo", this.siteinfo);
                ActionContext.getContext().put("message", "内容页地址采集完毕，3秒后自动进行下一步");
                ActionContext.getContext().put("urladdress", "/control/collect/collectbook");
                Logger.getAnonymousLogger().info("==================startcollect - go:collectbook");
            }
            return "automessage";
        }
        ActionContext.getContext().put("message", "获取内容页地址出错");
        ActionContext.getContext().put("urladdress", "/control/collect/index");

        return "message";
    }

    public String collectBook() {
        List contenturls =
                (List) ActionContext.getContext().getSession().get("contenturls");


        Map regs = (Map) ActionContext.getContext().getSession().get("regs");
        this.siteinfo = ((SiteInfo) ActionContext.getContext().getSession().get("siteinfo"));

        if ((contenturls == null) || (contenturls.size() == 0)) {
            int firstindex = 0;
            contenturls = this.contentUrlService.getScrollData(firstindex, this.maxresult,
                    "o.isdo=?1 and o.siteid=?2", new Object[]{Boolean.valueOf(false), this.siteinfo.getId()}).getResultlist();

            if ((contenturls == null) || (contenturls.size() == 0)) {
                ActionContext.getContext().put("message", "恭喜，所有小说采集完毕,点击确定采集章节内容");
                ActionContext.getContext().put("urladdress", "/control/collect/collectchapter");
            } else {
                ActionContext.getContext().put("message", "3秒后自动采集");
                ActionContext.getContext().put("urladdress", "/control/collect/collectbook");
            }
        } else {
            Logger.getAnonymousLogger().info("==================contenturls:" + contenturls.size());
            ContentUrl contentUrl = (ContentUrl) contenturls.get(0);
            Logger.getAnonymousLogger().info("==================contentUrl:" + contentUrl.getUrl() + ",开始采集url");
            BookInfo book = BookCollectUtil.getBookInfos(contentUrl.getUrl(),
                    this.siteinfo.getCharset(), this.siteinfo.getDomain(), regs,
                    null, null);
            if (book != null) {
                book.setSiteid(this.siteinfo.getId());

                this.bookInfoService.save(book);

                Map chapterTitlesAndUrls =
                        BookCollectUtil.getChapterTitleAndUrl(this.siteinfo.getChaptertitleAndUrlReg(),
                                this.siteInfoService.getListOldStr(this.siteinfo),
                                this.siteInfoService.getListNewStr(this.siteinfo), book.getResourceUrl().replace("http://", "") + "/", this.siteinfo.getCharset());// this.siteinfo.getDomain()-》 contentUrl.getUrl()

                Logger.getAnonymousLogger().info("==================chapterTitlesAndUrls" + chapterTitlesAndUrls.size());

                this.bookChapterService.addChaptersByMap(chapterTitlesAndUrls, book.getId(), this.siteinfo.getId());

                ActionContext.getContext().put("message", book.getTitle() + "采集完毕，3秒后进入下一部小说的采集");
                ActionContext.getContext().put("urladdress", "/control/collect/collectbook");
            } else {
                ActionContext.getContext().put("message", "采集失败，3秒后进入下一部小说的采集");
                ActionContext.getContext().put("urladdress", "/control/collect/collectbook");
            }

            contentUrl.setIsdo(true);
            this.contentUrlService.update(contentUrl);
            contenturls.remove(0);
        }

        ActionContext.getContext().getSession().put("contenturls", contenturls);
        return "automessage";
    }

    public String collectChapterContent() {
        List bookChapters =
                (List) ActionContext.getContext().getSession().get("bookChapters");
        Logger.getAnonymousLogger().info("===============================================================" +
                "collectChapterContent：" + (bookChapters != null ? bookChapters.size() : ""));

        this.siteinfo = ((SiteInfo) ActionContext.getContext().getSession().get("siteinfo"));
        Logger.getAnonymousLogger().info("===============================================================" +
                "collectChapterContent - siteinfo：" + siteinfo.getDomain() + ",id:" + siteinfo.getId());
        if ((bookChapters == null) || (bookChapters.size() == 0))//如果数据库为空，查询出来，然后后面放入session
        {
            Logger.getAnonymousLogger().info("===============================================================" +
                    "collectChapterContent - siteinfo：" + siteinfo.getDomain());
            int firstindex = 0;
            bookChapters = this.bookChapterService.getScrollData(firstindex, this.maxresult,
                    "o.iscollected=?1 and o.siteid=?2", new Object[]{Boolean.valueOf(false), this.siteinfo.getId()}).getResultlist();
            if ((bookChapters == null) || (bookChapters.size() == 0)) {//数据库 和 session均无待采集章节
                ActionContext.getContext().put("message", "恭喜，所有章节内容采集完毕,数据库 和 session均无待采集章节");
                ActionContext.getContext().put("urladdress", "/control/collect/index");
            } else {
                ActionContext.getContext().put("message", "3秒后自动开始采集章节内容");
                ActionContext.getContext().put("urladdress", "/control/collect/collectchapter");
            }
        } else {//session 中有值，开始采集
            Logger.getAnonymousLogger().info("==================collectChapterContent - bookChapters:" + bookChapters.size());
            BookChapter chapter = (BookChapter) bookChapters.get(0);

            String content = BookCollectUtil.getWebContentByUrl(chapter.getResourceUrl(), this.siteinfo.getCharset());
            // 这里为空的特殊处理，重新采集
            if(content==null || "".equals(content)){
                bookChapters.remove(0);//忽略 采集下一个
                ActionContext.getContext().put("message", chapter.getTitle() + "采集<font color='red'>失败</font>，3秒后自动进入下一章节的采集");
                ActionContext.getContext().put("urladdress", "/control/collect/collectchapter");
                return "automessage";
            }
            //       String chapterContent = BookCollectUtil.getContentByReg(this.siteinfo.getChapterContentReg(),
//         content, this.siteInfoService.getListOldStr(this.siteinfo), this.siteInfoService.getListNewStr(this.siteinfo));

            String chapterContent = HtmlJsoupUtil.getMaxContentEl(content).html();
            chapterContent = BookCollectUtil.replaceStr(chapterContent,
                    this.siteInfoService.getListOldStr(this.siteinfo), this.siteInfoService.getListNewStr(this.siteinfo));
            Logger.getAnonymousLogger().info("==================collectChapterContent - :content" + content.length() + " -- chapterContent" + chapterContent + "---" + this.siteinfo.getChapterContentReg());

            if ((chapterContent == null) || ("".equals(chapterContent))) {
                ActionContext.getContext().put("message", chapter.getTitle() + "采集<font color='red'>失败</font>，3秒后自动进入下一章节的采集");
                ActionContext.getContext().put("urladdress", "/control/collect/collectchapter");
            } else {
                String filepath = ServletActionContext.getServletContext().getRealPath(CommonConstant.TXTPATH) +
                        CommonConstant.DELIMITER + chapter.getBookid() +
                        CommonConstant.DELIMITER + chapter.getId() +
                        ".txt";
                CommonUtil.saveStringToFile(filepath, chapterContent, this.siteinfo.getCharset());

                ActionContext.getContext().put("message", chapter.getTitle() + "采集完毕，3秒后自动进入下一章节的采集");
                ActionContext.getContext().put("urladdress", "/control/collect/collectchapter");
            }

            chapter.setIscollected(true);
            this.bookChapterService.update(chapter);
            bookChapters.remove(0);
        }

        ActionContext.getContext().getSession().put("siteinfo", this.siteinfo);
        ActionContext.getContext().getSession().put("bookChapters", bookChapters);
        Logger.getAnonymousLogger().info("==================collectChapterContent - :put bookChapters" +
                bookChapters.size());

        return "automessage";
    }
}

/* Location:           C:\work\apache-tomcat-7.0.55\webapps\ROOT\WEB-INF\classes\
 * Qualified Name:     com.wzh.action.control.collect.CollectAction
 * JD-Core Version:    0.6.1
 */